const fs = require("fs");
const logger = require("./logger");

const cacheKey = "args";
const cache = new Map();

function clearCache() {
  cache.delete(cacheKey);
}

function readArgs() {
  if (cache.has(cacheKey)) {
    return cache.get(cacheKey);
  }
  try {
    // 参数获取，读取 args.json 文件的内容并转换为字符串
    let argsInfo = fs.readFileSync("args.json").toString();
    // 尝试将读取到的字符串解析为 JSON 对象
    const args = JSON.parse(argsInfo);
    cache.set(cacheKey, args);
    return args;
  } catch (e) {
    console.log("读取参数发生异常：------------->", e);
    logger.info("readArgs------------->", e);
  }
}

module.exports = {
  readArgs,
  clearCache,
};
